<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once ("database.php");

class photo_handler extends database{

        public $result;

	function __construct(){
		parent::__construct();
	}

        function  __destruct() {

        }


        function upload_photo($usr_id, $url, $tag, $desc, $date_c, $date_u, $category, $priv, $com_permission){
            $query = "INSERT INTO photo (usr_owner,url,tag,description,date_capt,date_upload,category,privacy,com_permission)
                       VALUES('$usr_id', '$url', '$tag', '$desc', '$date_c', '$date_u', '$category', '$priv', $com_permission)";

            $this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
        }

        function delete_photo($photo_id){
            $query = "DELETE FROM photo WHERE photo_id=$photo_id";

			//delete beneran fotonya
			$q = "SELECT url FROM photo WHERE photo_id=$photo_id";
			$this->exec_q($q,"url");
			$url = $this->result[0];
			unlink($url);
			
			$this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
			$this->del_allpeople_tagged($photo_id);
			
			
        }

        function get_url($photo_id){
             $q = "SELECT url from photo WHERE photo_id=$photo_id";
            $this->exec_q($q, "url");
        }

        function edit_photo_details($photo_id, $type, $val){
            if ($type == 1){
                $query = "UPDATE photo SET tag='$val' WHERE photo_id='$photo_id'";
            }
            else if ($type == 2){
                $query = "UPDATE photo SET description='$val' WHERE photo_id='$photo_id'";
            }
            else if ($type == 3){
                $query = "UPDATE photo SET date_capt='$val' WHERE photo_id='$photo_id'";
            }
            else if ($type == 4){
                $query = "UPDATE photo SET date_upload='$val' WHERE photo_id='$photo_id'";
            }
            else if ($type == 5){
                $query = "UPDATE photo SET category='$val' WHERE photo_id='$photo_id'";
            }
            else if ($type == 6){
                $query = "UPDATE photo SET privacy='$val' WHERE photo_id='$photo_id'";
            }
            else if ($type == 7){
                $query = "UPDATE photo SET com_permission='$val' WHERE photo_id='$photo_id'";
            }
			else if ($type == 8){
                $query = "UPDATE photo SET rating='$val' WHERE photo_id='$photo_id'";
            }
            else {
                $query = "";
            }

            $this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
        }
		
		function retr_photo_tag($photo_id) {
			$query = "SELECT * FROM tagged_in_photo WHERE photo_id = '$photo_id'";
			$this->exec_q($query, "usr_email");
		}
		
		function retr_photo_rating($photo_id) {
			$query = "SELECT * FROM photo WHERE photo_id = '$photo_id'";
			$this->exec_q($query, "rating");
		}
		
		function retr_photo_owner($photo_id) {
			$query = "SELECT * FROM photo WHERE photo_id = '$photo_id'";
			$this->exec_q($query, "usr_owner");
		}

        function retr_photo_details($photo_id, $type){
            $query = "SELECT * FROM photo WHERE photo_id = '$photo_id'";

            if ($type == 1){
               $res = $this->exec_q($query, "tag");
            }
            else if ($type == 2){
               $res = $this->exec_q($query, "desription");
            }
            else if ($type == 3){
               $res = $this->exec_q($query, "date_capt");
            }
            else if ($type == 4){
               $res = $this->exec_q($query, "date_upload");
            }
            else if ($type == 5){
               $res = $this->exec_q($query, "category");
            }
            else if ($type == 6){
               $res = $this->exec_q($query, "privacy");
            }
            else if ($type == 7){
               $res = $this->exec_q($query, "com_permission");
            }
            else if ($type == 8){ //retrieve all
               $res[0] = $this->exec_q($query, "tag");
               $res[1] = $this->exec_q($query, "description");
               $res[2] = $this->exec_q($query, "date_capt");
               $res[3] = $this->exec_q($query, "date_upload");
               $res[4] = $this->exec_q($query, "category");
               $res[5] = $this->exec_q($query, "privacy");
               $res[6] = $this->exec_q($query, "com_permission");
               $res[7] = $this->exec_q($query, "rating");
               $res[8] = $this->exec_q($query, "usr_owner");
            }


            $this->result = $res;
        }


		function retr_tagged_photo($usr_id) {
			$query = "SELECT * FROM tagged_in_photo WHERE usr_email = '$usr_id'";
			$idid = $this->exec_q($query,"photo_id");
			if ($idid[0]!=null) {
				$iter=0;
				for($i=0;$i<count($idid);$i++) {
					$photo_id = $idid[$i];
					$this->get_url($photo_id);
					$owner_photo = new photo_handler();
					$owner_photo->retr_photo_owner($photo_id);
					if ($usr_id!=$owner_photo->result[0]) {
						$res[0][$iter] = $photo_id;		//id						
						$res[1][$iter] = $this->result[0];	//url
						$iter++;
					}
				}
				$this->result = $res;
			} else
				$this->result = null;
		}

        function add_people_tagged($photo_id, $usr_id){
            $query = "INSERT INTO tagged_in_photo (photo_id,usr_email) VALUES('$photo_id', '$usr_id')";
            $this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
        }

        function del_people_tagged($photo_id, $usr_id){
            $query = "DELETE FROM tagged_in_photo WHERE photo_id='$photo_id' AND usr_email='$usr_id'";

            $this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
        }
		
		function del_allpeople_tagged($photo_id) {
		    $query = "DELETE FROM tagged_in_photo WHERE photo_id='$photo_id'";

            $this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
		}

        function add_comment($photo_id, $usr_id, $body){
            $q_order = "SELECT max(order_photo) AS maks FROM photo_comment WHERE photo_id=$photo_id";
            $this->connect();
            $max_order = mysql_result(mysql_query($q_order), "maks");
            $m = $max_order;
            $m++;

            if ($max_order == NULL) $m=1;
            
            mysql_close();
            $query = "INSERT INTO photo_comment (photo_id, usr_commented, comment_body, order_photo) VALUES('$photo_id', '$usr_id', '$body', '$m')";

           	$this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
        }

        function del_comment($photo_comment_id){
            $query = "DELETE FROM photo_comment WHERE photo_comment_id='$photo_comment_id'";

            $this->connect();
            mysql_query($query,$this->conn);
            mysql_close();
        }

        function increase_rating($photo_id){
            $query = "SELECT * FROM photo WHERE photo_id=$photo_id";
            $this->exec_q($query,"rating");
            $r = $this->result[0];
            $r++;
            
            $query_upd = "UPDATE photo SET rating=$r WHERE photo_id=$photo_id";

            $this->connect();
            mysql_query($query_upd,$this->conn);
            mysql_close();
			return $r;
        }

        function decrease_rating($photo_id){
            $query = "SELECT * FROM photo WHERE photo_id=$photo_id";
            $this->exec_q($query,"rating");
            $r = $this->result[0];
            $r--;
            
            $query_upd = "UPDATE photo SET rating=$r WHERE photo_id=$photo_id";

            $this->connect();
            mysql_query($query_upd,$this->conn);
            mysql_close();
			return $r;
        }

        function view_photo($photo_id){
            //result = owner, URL, people tagged, usr commented, comment body
            //get URL
            $query = "SELECT * FROM photo WHERE photo_id=$photo_id";
            $this->exec_q($query,"url");
            $res[0] = $this->result[0];
            //owner
            $this->exec_q($query,"usr_owner");
            $res[1] = $this->result[0];
            //people tagged
            $query2 = "SELECT * FROM tagged_in_photo WHERE photo_id=$photo_id";
            $this->exec_q($query2,"usr_email");

            for ($i=0; $i<count($this->result); $i++){
                $res[2][$i] = $this->result[$i];
            }

            //comment
            $query3 = "SELECT * FROM photo_comment WHERE photo_id=$photo_id ORDER BY order_photo";
            $this->exec_q($query3,"usr_commented");
            for ($i=0; $i<count($this->result); $i++){
                $res[3][$i] = $this->result[$i];
            }
            $this->result = NULL;
            $this->exec_q($query3,"comment_body");
            for ($i=0; $i<count($this->result); $i++){
                $res[4][$i] = $this->result[$i];
            }
			$this->result = NULL;
            $this->exec_q($query3,"photo_comment_id");
            for ($i=0; $i<count($this->result); $i++){
                $res[5][$i] = $this->result[$i];
            }

            $this->result = NULL; //hilangin yg sebelumnya
            $this->result = $res;
        }

        function view_all_user_photos($usr_id){
            $query = "SELECT url,photo_id FROM photo WHERE usr_owner='$usr_id'";
            $this->exec_q($query, "url");
            $url = $this->result;
            $this->exec_q($query, "photo_id");
            $id = $this->result;

            $this->result[0] = $id; //ID
            $this->result[1] = $url; //URL
        }
		
		function view_user_photos_category($usr_id,$hubungan){
			if ($hubungan=="everyone") {
            	$query = "SELECT url,photo_id FROM photo WHERE usr_owner='$usr_id' AND privacy='$hubungan'";
			} else if ($hubungan=="just followers") {
				$hubungan2="only me";
				$query = "SELECT url,photo_id FROM photo WHERE usr_owner='$usr_id' AND privacy NOT IN ('$hubungan2')";
			} else {
				// members only
				$hubungan2="only me";
				$hubungan3="just followers";
				$query = "SELECT url,photo_id FROM photo WHERE usr_owner='$usr_id' AND privacy NOT IN ('$hubungan2', '$hubungan3')";
			}
            $this->exec_q($query, "url");
            $url = $this->result;
            $this->exec_q($query, "photo_id");
            $id = $this->result;

            $this->result[0] = $id; //ID
            $this->result[1] = $url; //URL
        }
		
		function view_usertag_photos_category($usr_id,$hubungan) {
			$query = "SELECT * FROM tagged_in_photo WHERE usr_email = '$usr_id'";
			$idid = $this->exec_q($query,"photo_id");
			if ($idid[0]!=null) {
				$iter = 0;
				for($i=0;$i<count($idid);$i++) {
					$cek_privacy = new photo_handler();
					$cek_privacy->retr_photo_details($idid[$i],6);
					$cek_owner = new photo_handler();
					$cek_owner->retr_photo_owner($idid[$i]);
					if ($usr_id!=$cek_owner->result[0]) {
						if ($hubungan=="everyone") {
							if ($cek_privacy->result[0]=="everyone") {
								$this->get_url($idid[$i]);
								$res[0][$iter] = $idid[$i];		//id
								$res[1][$iter] = $this->result[0];	//url
								$iter++;
							}
						} else {
							if ($cek_privacy->result[0]=="everyone") {
								$this->get_url($idid[$i]);
								$res[0][$iter] = $idid[$i];		//id
								$res[1][$iter] = $this->result[0];	//url
								$iter++;
							} else if ($cek_privacy->result[0]=="only me") {
								
							} else if ($cek_privacy->result[0]=="just followers") {
								// cek user dengan pemilik foto apakah follower atau tidak
								// $hubungan diisi dengan id user yang melihat
								$cek_follow = new follower_handler();
								$this->retr_photo_owner($idid[$i]);
								if($cek_follow->is_follow($hubungan,$this->result[0])) {
									$this->get_url($idid[$i]);
									$res[0][$iter] = $idid[$i];		//id
									$res[1][$iter] = $this->result[0];	//url
									$iter++;
								}
							} else if ($cek_privacy->result[0]=="members only") {
								$this->get_url($idid[$i]);
								$res[0][$iter] = $idid[$i];		//id
								$res[1][$iter] = $this->result[0];	//url
								$iter++;
							}
						}
					}
				}
				$this->result = $res;
			} else {
				$this->result = null;
			}
		}

        function view_recent_photos($jml){
            //ambil id yang paling besar
			$everyone="everyone";
            $q_id = "SELECT max(photo_id) AS maks FROM photo";
            $q_jum = "SELECT * from photo";
			
            $this->exec_q($q_id, "maks");
			if ($this->result[0]!=null) {
				$max_id = $this->result[0];
	
				$this->exec_q($q_jum, "photo_id");
				$jumlah = count($this->result);
	
				//return 8 id terbesar
				if ($jumlah < $jml){
					$iter = $jumlah;
				}
				else{
				   $iter = $jml;
				}
	
				 //langsung return URL dari ke-8 gambar
					for ($i=0; $i<$iter; $i++){
						$q_url = "SELECT * FROM photo WHERE photo_id=$max_id";
						$this->exec_q($q_url, "privacy");
						if ($this->result[0]!=null) {
							if ($this->result[0]=="everyone") {
								$this->exec_q($q_url, "usr_owner");
								$url[0][$i] = $this->result[0];
								$this->exec_q($q_url, "url");
								$url[1][$i] = $this->result[0];
								$url[2][$i] = $max_id;

							} else {
								$i--;
							}
							$max_id--;
							if ($max_id==0)
								$i=$iter;
						} else {
							$i--;
							$max_id--;
							if ($max_id==0)
								$i=$iter;
						}
					}
	
			   $this->result = $url;
			}
        }
		
		function view_popular_photos(){
            //ambil foto dengan jumlah komen terbanyak
			$everyone="everyone";
            $q_jum = "SELECT count(photo_id) AS jumlah from photo";
			$q_id = "SELECT photo_id from photo";
			
			$this->exec_q($q_jum,"jumlah");
			
			$total = $this->result[0];
			//echo "ini total". $total;
				$this->exec_q($q_id,"photo_id");
				$res_id = $this->result;
			/*for($i = 0; $i< count($res_id) ; $i++)
			{
				echo $res_id[$i]. "<br />";
			}*/
			for ($i = 0; $i < $total; $i++ ){
				$q = "SELECT count(komen.comment_body) AS jml FROM (SELECT comment_body FROM photo_comment WHERE photo_id=$res_id[$i]) AS komen";
				$this->exec_q($q,"jml");
				$array_of_id[$i][0] = $res_id[$i];
				$array_of_id[$i][1] = $this->result[0];
				
			}
			//bubblesort
			for($iterator = 0; $iterator < $total; $iterator++)
				{
				for($jiterator = $total-1; $jiterator > $iterator; $jiterator--)
					{
					if($array_of_id[$jiterator - 1][1] < $array_of_id[$jiterator][1])
						{
						$temp = $array_of_id[$jiterator];
						$array_of_id[$jiterator] = $array_of_id[$jiterator-1];
						$array_of_id[$jiterator - 1] = $temp;
						}
					}
				}
			//echo
			/*for($i = 0; $i< $total ; $i++)
				{
				echo $array_of_id[$i][0]. $array_of_id[$i][1]. "<br />";
				}*/
				
			if ($total!=null) {
				$jumlah = count($array_of_id); //hitung jml photo id
	
				//return 8 id terbesar
				if ($jumlah < 6){
					$iter = $jumlah;
				}
				else{
				   $iter = 6;
				}
				 //langsung return URL dari ke-8 gambar
					for ($i=0; $i<$iter; $i++){
						$akalakalan = $array_of_id[$i][0];
						$q_url = "SELECT usr_owner,url,privacy FROM photo WHERE photo_id=$akalakalan";
						$this->exec_q($q_url, "privacy");
						//echo "<br />". $q_url. "<br />";
						if ($this->result[0]!=null) {
							if ($this->result[0]=="everyone") {
								$this->exec_q($q_url, "usr_owner");
								$url[0][$i] = $this->result[0];
								//echo $url[0][$i];
								$this->exec_q($q_url, "url");
								$url[1][$i] = $this->result[0];
                                                                $url[2][$i] = $akalakalan;
								//echo $url[1][$i]. "<br />";
							} else {
								$iter++;
							}
							/*$max_id--;
							if ($max_id==0)
								$i=$iter;*/
						} /*else {
							$i--;
							$max_id--;
							if ($max_id==0)
								$i=$iter;
						}*/
					}
			   $this->result = $url;
			}
        }
		
		function view_favorite_photos($jml){
            //query id dan rating
			$q_photo = "SELECT photo_id,rating FROM photo";
			$this->exec_q($q_photo,"photo_id");
			$id = $this->result;
			
			$this->exec_q($q_photo,"rating");
			$rating = $this->result;
			$total = count($id);
			//assign ke array
			for($i = 0; $i< count($id); $i++)
				{
				$array_of_id[$i][0] = $id[$i];
				$array_of_id[$i][1] = $rating[$i];
				}
			//array beres
			
			//bubblesort
			for($iterator = 0; $iterator < $total; $iterator++)
				{
				for($jiterator = $total-1; $jiterator > $iterator; $jiterator--)
					{
					if($array_of_id[$jiterator - 1][1] < $array_of_id[$jiterator][1])
						{
						$temp = $array_of_id[$jiterator];
						$array_of_id[$jiterator] = $array_of_id[$jiterator-1];
						$array_of_id[$jiterator - 1] = $temp;
						}
					}
				}
			//array terurut
			
			//mulai tampilkan
			
			if ($total!=null) {
				$jumlah = count($array_of_id); //hitung jml photo id
	
				//return 8 id terbesar
				if ($jumlah < 6){
					$iter = $jumlah;
				}
				else{
				   $iter = 6;
				}
				 //langsung return URL dari ke-8 gambar
					for ($i=0; $i<$iter; $i++){
						$akalakalan = $array_of_id[$i][0];
						//echo $akalakalan. "<br />";
						$q_url = "SELECT usr_owner,url,privacy FROM photo WHERE photo_id=$akalakalan";
						$this->exec_q($q_url, "privacy");
						//echo "<br />". $q_url. "<br />";
						if ($this->result[0]!=null) {
							if ($this->result[0]=="everyone") {
								$this->exec_q($q_url, "usr_owner");
								$url[0][$i] = $this->result[0];
								//echo $url[0][$i];
								$this->exec_q($q_url, "url");
								$url[1][$i] = $this->result[0];
                                                                $url[2][$i] = $akalakalan;
								//echo $url[1][$i]. "<br />";
							} else {
								$iter++;
							}
							/*$max_id--;
							if ($max_id==0)
								$i=$iter;*/
						} /*else {
							$i--;
							$max_id--;
							if ($max_id==0)
								$i=$iter;
						}*/
					}
			   $this->result = $url;
			}
        }
    }
	// $o = getcwd();
	// chdir("../");
	// unlink("images/photos/bangyo@yahoo.com/capture20110114090911.jpg");
	// chdir($o);
?>